package bhbt.controller.member.kuang;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import bhbt.controller.core.BaseController;
import bhbt.model.Daili;
import bhbt.model.Member;
import bhbt.model.ParamWage;
import bhbt.service.DailiService;
import bhbt.service.MemberService;
import bhbt.service.ParamWageService;
import bhbt.service.log.LogKuangService;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;





@Controller
 
public class kuangAgainController extends BaseController {

	@Resource
	private MemberService memberService;
	@Resource
	private LogKuangService logKuangService;
	@Resource
	private DailiService dailiService;
	
	@Resource
	private ParamWageService paramWageService;

	@RequestMapping("member/kuang/kuangAgainShow.shtml")
	public String show(HttpServletRequest request) throws Exception{
		
		//会员信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		request.setAttribute("member",member);
		
		Daili daili = this.dailiService.getByKey(member.getDailiId());
		request.setAttribute("daili",daili);
		
		ParamWage paramWage1002=paramWageService.getByKey(1002);
		ParamWage paramWage1003=paramWageService.getByKey(1003);
		request.setAttribute("paramWage1002",paramWage1002.getValue());
		request.setAttribute("paramWage1003",paramWage1003.getValue());
		return "member/kuang/kuangAgain";
	}
 
	@RequestMapping("member/kuang/kuangAgain.shtml")
	public String ti(HttpServletRequest request, ModelMap model) throws Exception{
		
		Boolean result=true;
		//会员信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		Daili daili = this.dailiService.getByKey(member.getDailiId());
		
		ParamWage paramWage1002=paramWageService.getByKey(1002);
		ParamWage paramWage1003=paramWageService.getByKey(1003);
		
		//是否可以继续拥有矿点
		ParamWage paramWage1004=paramWageService.getByKey(1004);
		 
		Map formMap = getParameterMap(request);
		String twoPassword = MapUtil.getString(formMap, "twoPassword");
		String pwd = CryptUtil.getInstance().encrypt2String(twoPassword,CryptUtil.keyStr);
   	 
    	if(!member.getTwoPassword().equals(pwd)){
    		setMessage(model,"二级密码不正确。");
    		result=false;
    	}else{
    		Integer num = daili.getKuangFuNum();	
			//买矿费用
			Double fei = num*paramWage1003.getValue();
			 
			if(member.getChuangye()<fei){
				setMessage(model,"复投币不足。");
        		result=false;
			}
			
			//判断是否还有资格拥有矿点
			
			if(member.getKuang()>=paramWage1004.getValue()||member.getBuyKuang()>=paramWage1004.getValue()){
				double xianKuangNum = paramWage1004.getValue();
				Integer xianKuangNumInt =(int)xianKuangNum;
				setMessage(model,"您已经购买或者拥有矿点超过："+xianKuangNumInt);
        		result=false;
			}
			
			
			
			if(result){
				 
				logKuangService.saveKuangAgain(num);
				setMessage(model,"复投成功。");
				 
			}
		
    		 
    	}
		
    	
		request.setAttribute("member",member);
		request.setAttribute("daili",daili);
		request.setAttribute("paramWage1002",paramWage1002.getValue());
		request.setAttribute("paramWage1003",paramWage1003.getValue());
		request.setAttribute("paramWage1004",paramWage1004.getValue());
		return "member/kuang/kuangAdd";
    	
	}
	
	 
	
}
